package com.contactmgr.activity;

import java.util.List;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.TabHost;

import com.contactmgr.R;
import com.contactmgr.logObjects.CallDataLog;
import com.contactmgr.logObjects.Contact;
import com.contactmgr.logObjects.Sms;
import com.contactmgr.utils.CallLogReader;
import com.contactmgr.utils.ContactNameResolver;
import com.contactmgr.utils.ContactReader;
import com.contactmgr.utils.ReadDataAppContext;
import com.contactmgr.utils.SmsReader;

public class DisplayLogActivity extends TabActivity implements OnGestureListener {
	private static final String SMS_LOG = "SmsLog";
	private static final String CALL_LOG = "CallLog";
	private static final String CONTACT = "Contact";
	List<Sms> inboxResolved;
	List<Sms> sentItemsResolved;
	List<Contact> contactList;
	List<CallDataLog> nameResolvedAllCallData;
	List<CallDataLog> nameResolvedMissedCallData;
	List<CallDataLog> nameResolvedIncomingCallData;
	List<CallDataLog> nameResolvedOutGoingCallData;
	private TabHost tabHost;
	private GestureDetector gestureScanner;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainlayout);
		gestureScanner = new GestureDetector(this);
		readLogs();
		showDataInTabsWithSeparateActivities();
	}

	private void readLogs() {

		contactList = ContactReader.getContacts(getContentResolver());

		List<CallDataLog> callLog = CallLogReader.getAllCallLogs(getContentResolver());
		List<CallDataLog> missedCallLog = CallLogReader.getMissedCallLogs(callLog);
		List<CallDataLog> incomingCallLog = CallLogReader.getIncomingCallLogs(callLog);
		List<CallDataLog> outGoingCallLog = CallLogReader.getOutGoingCallLogs(callLog);
		nameResolvedAllCallData = ContactNameResolver.resolveNamesInCallLog(contactList, callLog);
		nameResolvedMissedCallData = ContactNameResolver.resolveNamesInCallLog(contactList, missedCallLog);
		nameResolvedIncomingCallData = ContactNameResolver.resolveNamesInCallLog(contactList, incomingCallLog);
		nameResolvedOutGoingCallData = ContactNameResolver.resolveNamesInCallLog(contactList, outGoingCallLog);
		List<Sms> inbox = SmsReader.readInbox(getContentResolver());
		List<Sms> sentItems = SmsReader.readSentItems(getContentResolver());
		inboxResolved = ContactNameResolver.resolveNamesInSms(contactList, inbox);
		sentItemsResolved = ContactNameResolver.resolveNamesInSms(contactList, sentItems);

		// set in global app context
		((ReadDataAppContext) getApplicationContext()).setContactList(contactList);
		((ReadDataAppContext) getApplicationContext()).setNameResolvedAllCallData(nameResolvedAllCallData);
		((ReadDataAppContext) getApplicationContext()).setNameResolvedMissedCallData(nameResolvedMissedCallData);
		((ReadDataAppContext) getApplicationContext()).setNameResolvedIncomingCallData(nameResolvedIncomingCallData);
		((ReadDataAppContext) getApplicationContext()).setNameResolvedOutGoingCallData(nameResolvedOutGoingCallData);

		((ReadDataAppContext) getApplicationContext()).setInboxResolved(inboxResolved);
		((ReadDataAppContext) getApplicationContext()).setSentItemsResolved(sentItemsResolved);
		for (CallDataLog callDataLog : nameResolvedAllCallData) {
			System.out.println(callDataLog.toString());
		}
		for (Contact contact : contactList) {
			System.out.println(contact.toString());
		}
	}

	private void showDataInTabsWithSeparateActivities() {
		Resources res = getResources();
		tabHost = getTabHost();
		Intent smsIntent;
		Intent callLogIntent;
		Intent contactIntent;
		// Create an Intent to launch an Activity for the tab (to be reused)
		smsIntent = new Intent().setClass(this, SmsLogActivity.class);
		callLogIntent = new Intent().setClass(this, CallLogActivity.class);
		contactIntent = new Intent().setClass(this, ContactLogActivity.class);
		// add views to tab host
		tabHost.addTab(tabHost.newTabSpec(CONTACT).setIndicator(CONTACT, res.getDrawable(R.drawable.contact_icon)).setContent(contactIntent));
		tabHost.addTab(tabHost.newTabSpec(CALL_LOG).setIndicator(CALL_LOG, res.getDrawable(R.drawable.call_icon)).setContent(callLogIntent));
		tabHost.addTab(tabHost.newTabSpec(SMS_LOG).setIndicator(SMS_LOG, res.getDrawable(R.drawable.message_icon)).setContent(smsIntent));
		tabHost.setCurrentTab(0);
	}

	@Override
	public boolean onTouchEvent(MotionEvent me) {
		return gestureScanner.onTouchEvent(me);
	}

	@Override
	public boolean onDown(MotionEvent e) {
		return true;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		float dispX = e2.getX() - e1.getX();
		float dispY = e2.getY() - e1.getY();
		if (Math.abs(dispX) >= 200 && Math.abs(dispY) <= 100) {
			// swipe ok
			if (dispX > 0) {
				// L-R swipe
				System.out.println("L-R swipe");
				changeLtoR();
			} else {
				// R-L swipe
				System.out.println("R-L swipe");
				changeRtoL();
			}
			return true;
		}
		return false;
	}

	private void changeLtoR() {
		int curTab = tabHost.getCurrentTab();
		int noOfTabs = getTabHost().getTabWidget().getTabCount();
		int nextTab = ((curTab + 1) % noOfTabs);
		tabHost.setCurrentTab(nextTab);

	}

	private void changeRtoL() {
		int curTab = tabHost.getCurrentTab();
		int noOfTabs = getTabHost().getTabWidget().getTabCount();
		int nextTab = ((curTab + noOfTabs - 1) % noOfTabs);
		tabHost.setCurrentTab(nextTab);

	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {

//		tabHost.dispatchTouchEvent(ev);
		if (gestureScanner != null) {
			if (gestureScanner.onTouchEvent(ev)) {
				if (ev.getY() > 100) {
					return true;
				}
			}
		}
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		return true;
	}

	@Override
	public void onShowPress(MotionEvent e) {
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}

}
